home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / aros / source / exec / memory / src / initstruct.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-16  |  4.7 KB  |  247 lines

  1. /*
  2.     $Id$
  3.     $Log$
  4.     Desc:
  5.     Lang: english
  6. */
  7. #include "exec_intern.h"
  8. #include "memory.h"
  9. #include <aros/machine.h>
  10. #include <exec/alerts.h>
  11.  
  12. /*****************************************************************************
  13.  
  14.     NAME */
  15.     #include <clib/exec_protos.h>
  16.  
  17.     __AROS_LH3(void, InitStruct,
  18.  
  19. /*  SYNOPSIS */
  20.     __AROS_LA(APTR,  initTable, A1),
  21.     __AROS_LA(APTR,  memory,    A2),
  22.     __AROS_LA(ULONG, size,      D0),
  23.  
  24. /*  LOCATION */
  25.     struct ExecBase *, SysBase, 13, Exec)
  26.  
  27. /*  FUNCTION
  28.     Initialize some library base or other structure depending on the
  29.     information in the init table. The init table consists of
  30.     instructions starting with an action byte followed by more
  31.     information. The instruction byte looks like:
  32.  
  33.     iisscccc where ii is the instruction code:
  34.               0 - copy following c+1 elements
  35.               1 - repeat following element c+1 times
  36.               2 - take next byte as offset, then copy
  37.               3 - take the next 3 bytes (in the machine's
  38.                   particular byte ordering) as offset, then
  39.                   copy
  40.                ss is the element size
  41.               0 - LONGs
  42.               1 - WORDs
  43.               2 - BYTEs
  44.                cccc is the element count-1
  45.  
  46.     Instruction bytes must follow the same alignement restrictions as LONGs,
  47.     the following elements are aligned to their particular restrictions.
  48.  
  49.     A 0 instruction ends the init table.
  50.  
  51.     INPUTS
  52.     initTable - Pointer to init table.
  53.     memory      - Pointer to uninitialized structure.
  54.     size      - Size of memory area to 0 out before decoding or 0
  55.             for no filling.
  56.  
  57.     RESULT
  58.  
  59.     NOTES
  60.  
  61.     EXAMPLE
  62.  
  63.     BUGS
  64.  
  65.     SEE ALSO
  66.  
  67.     INTERNALS
  68.  
  69.     HISTORY
  70.  
  71. ******************************************************************************/
  72. {
  73.     __AROS_FUNC_INIT
  74.  
  75.     LONG  cnt;
  76.     ULONG offset=0,src;
  77.     UBYTE *it,*dst;
  78.     int   s,t;
  79.  
  80.     /* Clear Memory area fast. Get number of longs and clear them. */
  81.     cnt=size/sizeof(LONG);
  82.     size&=(sizeof(LONG)-1);
  83.     dst=(UBYTE *)memory;
  84.     if(cnt)
  85.     do
  86.     {
  87.         *(LONG *)dst=0;
  88.         dst+=sizeof(LONG);
  89.     }
  90.     while(--cnt);
  91.  
  92.     /* Clear the rest. */
  93.     cnt=size;
  94.     if(cnt)
  95.     do
  96.         *dst++=0;
  97.     while(--cnt);
  98.  
  99.     it =(UBYTE *)initTable;
  100.     dst=(UBYTE *)memory;
  101.  
  102.     /* As long as there's something to do */
  103.     while(*it!=0)
  104.     {
  105.     /* What to do. */
  106.     t=*it>>6&3;
  107.  
  108.     /* Element size. */
  109.     s=*it>>4&3;
  110.  
  111.     /* Number of things to do (-1). */
  112.     cnt=*it&15;
  113.  
  114.     /* Depending on the action there may be more information */
  115.     switch(t)
  116.     {
  117.         case 0:
  118.         case 1:
  119.         /* Skip the action byte */
  120.         it++;
  121.         break;
  122.         case 2:
  123.         /* Skip the action byte, get the offset */
  124.         it++;
  125.         offset=*it++;
  126.         break;
  127.         case 3:
  128.         /*
  129.             Get 24bit offset. It's the programmer's responsibility
  130.             to align the action byte with a LONG instruction before
  131.             this.
  132.         */
  133. #if BIG_ENDIAN
  134.         offset=*(ULONG *)it&0xffffff;
  135. #else
  136.         offset=*(ULONG *)it>>8;
  137. #endif
  138.         it+=sizeof(LONG);
  139.         break;
  140.     }
  141.  
  142.     /* Align source and destination pointers */
  143.     switch(s)
  144.     {
  145.         case 0:
  146.         /* Align pointer to LONG requirements */
  147.         it =(UBYTE *)(((ULONG)it +LONGALIGN-1)&~(LONGALIGN-1));
  148.         dst=(UBYTE *)(((ULONG)dst+LONGALIGN-1)&~(LONGALIGN-1));
  149.         break;
  150.         case 1:
  151.         /* Same for WORDs */
  152.         it =(UBYTE *)(((ULONG)it +WORDALIGN-1)&~(WORDALIGN-1));
  153.         dst=(UBYTE *)(((ULONG)dst+WORDALIGN-1)&~(WORDALIGN-1));
  154.         break;
  155.         case 2:
  156.         /* Nothing to do for bytes */
  157.         break;
  158.         default:
  159.         /* And an Alert for nibbles ;-) */
  160.         Alert(ACPU_AddressErr);
  161.  
  162.         /*
  163.             Tell the compiler that he doesn't need to
  164.             care about side effects of Alert()
  165.         */
  166.         return;
  167.     }
  168.  
  169.     /* Switch over action */
  170.     switch(t)
  171.     {
  172.         case 2:
  173.         case 3:
  174.         /* Action is: Add offset then copy */
  175.         dst=(BYTE *)memory+offset;
  176.  
  177.         /* Fall through */
  178.         case 0:
  179.         /* Action is: Copy the next <cnt> elements to the current location */
  180.         switch(s)
  181.         {
  182.             case 0:
  183.             /* Copy loop */
  184.             do
  185.             {
  186.                 *(LONG *)dst=*(LONG *)it;
  187.                 dst+=sizeof(LONG);
  188.                 it +=sizeof(LONG);
  189.             }while(--cnt>=0);
  190.             break;
  191.             case 1:
  192.             do
  193.             {
  194.                 *(WORD *)dst=*(WORD *)it;
  195.                 dst+=sizeof(WORD);
  196.                 it +=sizeof(WORD);
  197.             }while(--cnt>=0);
  198.             break;
  199.             case 2:
  200.             do
  201.                 *dst++=*it++;
  202.             while(--cnt>=0);
  203.             break;
  204.         }
  205.         break;
  206.         case 1:
  207.         /* Action is: Repeat the next element <cnt> times */
  208.         switch(s)
  209.         {
  210.             case 0:
  211.             /* Get source */
  212.             src=*(LONG *)it;
  213.             it +=sizeof(LONG);
  214.  
  215.             /* And write it. */
  216.             do
  217.             {
  218.                 *(LONG *)dst=src;
  219.                 dst+=sizeof(LONG);
  220.             }while(--cnt>=0);
  221.             break;
  222.             case 1:
  223.             src=*(WORD *)it;
  224.             it +=sizeof(WORD);
  225.             do
  226.             {
  227.                 *(WORD *)dst=src;
  228.                 dst+=sizeof(WORD);
  229.             }while(--cnt>=0);
  230.             break;
  231.             case 2:
  232.             src=*it++;
  233.             do
  234.                 *dst++=src;
  235.             while(--cnt>=0);
  236.             break;
  237.         }
  238.         break;
  239.     }
  240.  
  241.     /* Align next instruction byte */
  242.     it=(UBYTE *)(((ULONG)it+LONGALIGN-1)&~(LONGALIGN-1));
  243.     }
  244.     __AROS_FUNC_EXIT
  245. }
  246.  
  247.